<?php
class Data {
	private $db_id;
	private $querynum = 0;
	private $dbname = '';
	private $pconnect = null;

	public function __construct() {
		if ($this->pconnect) {
			if(!$this->db_id=@mysql_pconnect(HOSTADDR, HOSTUSER, HOSTPASS,true)) {
				$this->mysql_errormsg('Can not connect to MySQL server');
			}
		} else {
			
			if(!$this->db_id=@mysql_connect(HOSTADDR, HOSTUSER, HOSTPASS,true)) {
				$this->mysql_errormsg('Can not connect to MySQL server');
			}
		}
		$this->dbname = DATABASE;
		@mysql_query("set names 'utf8'");
		@mysql_select_db(DATABASE);
	}
	
	public function setPconnect($pconnect = 0) {
		$this->pconnect = $pconnect;
	}
	
	function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0) {
		if($pconnect) {
			if(!$this->db_id=@mysql_pconnect($dbhost, $dbuser, $dbpw,true)) {
				$this->mysql_errormsg('Can not connect to MySQL server');
			}
		} else {
			if(!$this->db_id=@mysql_connect($dbhost, $dbuser, $dbpw,true)) {
				$this->mysql_errormsg('Can not connect to MySQL server');
			}
		}
		$this->dbname = $dbname;
		@mysql_query("set names 'utf8'");
		@mysql_select_db($dbname);
	}

	function select_db($dbname='') {
		if($dbname == '') {
			return @mysql_select_db($this->dbname,$this->db_id);
		}
		return @mysql_select_db($dbname,$this->db_id);
	}

	function query($sql) {		
		if(!($query = @mysql_query($sql,$this->db_id))) {
				$this->mysql_errormsg('MySQL Query Error', $sql);
			}		
		$this->querynum++;		
		return $query;
	}
	
	function fetch_array($query, $result_type = MYSQL_ASSOC) {
		return @mysql_fetch_array($query, $result_type);
	}

	function fetch_row($query) {
		$query = @mysql_fetch_row($query);
		return $query;
	}
	
	function num_rows($query) {
		$query = @mysql_num_rows($query);
		return $query;
	}

	function insert_id() {
		$id = @mysql_insert_id($this->db_id);
		return $id;
	}
	
	function free_result($query) {
		return @mysql_free_result($query);
	}
	
	function close() {
		return @mysql_close($this->db_id);
	}
	
	function mysql_errormsg($message = '', $sql = '') {
		echo $message."<br>".$sql."<br>";
		echo $this->errno()." ".$this->error();
		exit;
	}

	function affected_rows() {
		return @mysql_affected_rows($this->db_id);
		//		传回最后查询为INSERT、UPDATE或DELETE所影响的列数目		
	}

	function error() {
		return @mysql_error($this->db_id);
		//从先前MySQL操作传回错误讯息
	}

	function errno() {
		return @intval(mysql_errno($this->db_id));
		//从先前MySQL操作传回错误讯息代号
	}

	function result($query, $row) {
		$query = @mysql_result($query, $row);
		return $query;
		//从MySQL结果传回一格(cell)的内容,效率低.
	}

	function num_fields($query) {
		return @mysql_num_fields($query);
		//传回结果中栏位的数目
	}	
	
	public function find($table, $fields, $where, $order, $limit) {
		$sql = "SELECT {$fields} FROM {$table} WHERE 1=1  ";
		if($where) $sql .= " AND $where ";
		if($order) $sql .= " ORDER BY {$order} ";
		if($limit) $sql .= " LIMIT {$limit} ";
		
		$query=$this->query($sql);
		$resultArr = array();
		$row = array();
		while ($row = $this->fetch_array($query)) {
			$resultArr[] = $row;
		}
		return $resultArr;
    }
    
  	public function fetchOne($table, $fields, $where, $order, $limit) {
  		$resultArr = $this->find($table, $fields, $where, $order, $limit);
  		return count($resultArr) > 0 ? $resultArr[0] : null;
  	}
  	public function fetchAllByQuery($query) {
		$resultArr = array();
		$row = array();
		while ($row = $this->fetch_array($query)) {
			$resultArr[] = $row;
		}
		return $resultArr;
    }
  	public function fetchOneByQuery($query) {
  		$resultArr = $this->fetchAllByQuery($query);
  		return count($resultArr) > 0 ? $resultArr[0] : null;
  	}
	
	public function insertData($table, $options) { //插入函数，参数为 table + 字段和字段所对应的键值
		if(!is_array($options)) {
			return false;
		}
		
		foreach($options as $i => $v) {
			$field[] = $i;
			$values[] = $v;
		}
	
		return $this->_insert($table, $field, $values);
	}
	
	/**
		$options = array('key'=>array('id' =>'11'),
						  'data'=>array('key'=>'value','key2'=>'value2'));
	**/
	function updateData($table, $options, $limit=1) {
		if(!is_array($options)) {
			return false;
		}
		
		$key = $options['key'];
		$data = $options['data'];
		
		$sql = "UPDATE {$table} SET";
		$sqlSub = '';
		foreach( $data as $i=>$v ) {
			$sqlSub .=", {$i} = '{$v}'";
		}
		$sql .= substr($sqlSub, 1 , strlen($sqlSub));
		$sql .=" WHERE 1=1 " ;
		$sqlSub = "";
		foreach ( $key as $i=>$v ) {
			$sqlSub .=" AND {$i} = '{$v}'";
		}
		$sql .= $sqlSub;
		if ( $limit > 0 ) {
			$sql .= "limit {$limit}";
		}
		return $this->query($sql);
	}
	
	/*
	$options = array('key'=>$value,'key1'=>$value1)
	*/
	function delData($table, $options, $limit=1) {
		$sql = "DELETE FROM {$table} WHERE 1=1 ";
		foreach ( $options as $i => $v ) {
			$sql .= "AND $i => '{$v}' ";
		}
		if ( $limit > 0 ) {
			$sql .= "limit {$limit}";
		}
		return $this->$this->query($sql);
	}	
	
	private function _insert($table, $field, $values) {
		$sqlSub = null;
		$sql = "INSERT INTO {$table} (";
		foreach ($field as $v) {
			$sqlSub .= ", {$v}";
		}
		$sql .= substr($sqlSub, 1 , strlen($sqlSub)) . " )";
		$sqlSub = null;
		foreach ( $values as $v ) {
			$sqlSub .= ", '{$v}' ";
		}
		$sql .= " VALUES (".substr($sqlSub, 1 , strlen($sqlSub)) .")";
		$this->query($sql);
		
		return $this->insert_id();
	}
}
?>
